//
//  AboutUsViewModel.swift
//  Exchange
//
//  Created by rui on 2018/8/2.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AboutUsViewModel: BaseViewModel {

    var updateInfo = [String:Any]()
    
    var isNewVersion = false
 
    var lastVersion: String = UIDevice.current.appVersion

    ///版本更新
    private lazy var updateVersionApi: UpdateVersionRequest = {
        let api = UpdateVersionRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    func updateApp() {
        if updateInfo.count <= 0 {
            CYMBShow.showMessage("已经是最新版本")
            return
        }
        let dict = updateInfo
       
        if self.isNewVersion == false {
            CYMBShow.showMessage("已经是最新版本")
            return
        }
        ///当前服务器最新版本
        let newestVersion = dict["newestVersion"] as? String
        
        ///当前本地版本
        let localVersion = UIDevice.current.appVersion
        
        ///强制更新的最低版本
        let forceUpdate = dict["forceUpdate"] as? String
        
        ///是否强制更新
        let isForce = localVersion.compare(forceUpdate ?? "") != .orderedDescending
        
        let msg = dict["versionDesc"] as? String
    
        let alertVc = UpgradeAlert(describe: msg ?? "", verson: newestVersion ?? "",isUpgradeBool:isForce)
        
        let urlStr = dict["url"] as? String
        
        alertVc.downBlock = {
            if let url = URL(string: urlStr ?? ""),UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.openURL(url)
            }
        }
        
        alertVc.cancelBlock = {
            
        }
    }
    
    func loadVersionInfo() {
        self.updateVersionApi.loadData()
    }
    
    func loadUpdateInfo() {
        if updateInfo.count <= 0 {
            return
        }
        let dict = updateInfo
        ///当前服务器最新版本
        let newestVersion = dict["newestVersion"] as? String
        ///当前本地版本
        let localVersion = UIDevice.current.appVersion
        ///是否有新版本
        let isNewVersion = localVersion.compare(newestVersion ?? "") == .orderedAscending
        self.isNewVersion = isNewVersion
        
        if let version = newestVersion {
            self.lastVersion = version
        }
        
        guard let vc = controller as? AboutUsViewController else { return  }
        vc.tableView.reloadData()
    }
    
}

extension AboutUsViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
       if let _ = request as? UpdateVersionRequest {
            if let data = data?["data"] as? [String:Any] {
                updateInfo = data
                self.loadUpdateInfo()
                self.response?(ResponseState.home_update_info,true, nil)
            }else {
                self.response?(ResponseState.home_update_info,false, nil)
            }
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? UpdateVersionRequest {
            self.response?(ResponseState.home_update_info,false, request.errorMsg)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? UpdateVersionRequest {
            if Bundle.main.infoDictionary != nil {
                if Bundle.main.infoDictionary!["CFBundleDisplayName"] != nil {
                    let projectName =   Bundle.main.infoDictionary!["CFBundleDisplayName"]!
                    pramas.updateValue(projectName, forKey: "name")
                }
            }
            pramas.updateValue("ios", forKey: "type")
        }
        return pramas
    }
    
}
